package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkMoreSpecific

import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog.DialogSpecificGrammarAnalysis
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.itemtouchhelper.JumpActivityHelp
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletGrammarSpecificListInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TopicGrammarKlgInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import kotlinx.android.synthetic.main.aibk_activity_more_specific.*
import org.koin.androidx.viewmodel.ext.android.viewModel

/*
* 更多专项
* */
class AibkMoreSpecificGrammarActivity :KBaseActivity(), View.OnClickListener {

    private var stuId = ""
    companion object {
        fun launch(activity: AppCompatActivity,stuId:String) {
            val intent = Intent(activity, AibkMoreSpecificGrammarActivity::class.java)
//            intent.putExtra("From", from)
            intent.putExtra("StuID", stuId)
//            intent.putExtra("TempletType",templetType)
            activity.startActivityForResult(intent,101)
        }
    }

    private val mViewModel: MarkViewModel by viewModel()
    private var topicGrammarKlgInfor: TopicGrammarKlgInfor?=null//获取语法专项分析知识点概况
    private var templetGrammarSpecificListInfor: TempletGrammarSpecificListInfor?=null

    override fun setLayout(): Int {
      return R.layout.aibk_activity_more_specific
    }

    override fun initView() {
        IvBack.setOnClickListener(this)

        RvMoreSpecific.recycledViewPool.clear()
        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        RvMoreSpecific.layoutManager = manager

        val adapterParent = AibkMoreSpecificGrammarParentAdapter(
                this,templetGrammarSpecificListInfor!!
        )
        adapterParent.setClickParentListener(object : AibkMoreSpecificGrammarParentAdapter.OnClickListener {
            override fun onClickSingleAnalysis(indexA: Int, indexB: Int) {
                getGrammarTopicKlg(indexA,indexB,false)
            }

            override fun onClickAllAnalysis(indexA: Int, indexB: Int) {
                getGrammarTopicKlg(indexA,indexB,true)
            }

        })
        RvMoreSpecific.adapter = adapterParent
    }

    override fun initData() {
        stuId = intent.getStringExtra("StuID")
        templetGrammarSpecificListInfor = JumpActivityHelp.mTempletGrammarSpecificListInfor
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.IvBack ->{
                setResult(101)
                finish()
            }
        }
    }

    private fun getGrammarTopicKlg(indexA: Int, indexB: Int,isAllAnalysis:Boolean) {

        var topicIdsX = ""
        if(isAllAnalysis){
            val templetGrammarSpecificListInforSize = templetGrammarSpecificListInfor!!.size
            for(i in 0 until  templetGrammarSpecificListInforSize){
                val subTopicListSize = templetGrammarSpecificListInfor!![i].SubTopicList.size
                for(j in 0 until subTopicListSize){
                    val subTopicId = templetGrammarSpecificListInfor!![i].SubTopicList[j].SubTopicID
                    if(topicIdsX.isEmpty()){
                        topicIdsX = subTopicId
                    }else{
                        topicIdsX = "$topicIdsX|$subTopicId"
                    }
                }
            }
        }else {
            topicIdsX = templetGrammarSpecificListInfor!![indexA].SubTopicList[indexB].SubTopicID
        }
        mViewModel.getTopicKlg(stuId, topicIdsX)
                .doOnSubscribe {
                    startLoad()
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        stopLoad()
                        topicGrammarKlgInfor = it.data
                        val dialogSpecificAnalysis = DialogSpecificGrammarAnalysis(
                                this@AibkMoreSpecificGrammarActivity,
                                this@AibkMoreSpecificGrammarActivity,
                                templetGrammarSpecificListInfor!!,
                                indexA,
                                indexB,
                                isAllAnalysis,
                                topicGrammarKlgInfor!!)
                        dialogSpecificAnalysis.show()
                    } else {
                        MyToast.showToast(it.message)
                    }
                }, {
                    MyToast.showToast(it.message)
                })
    }

}